package dbdata

import (
	"reflect"
	"github.com/astaxie/beego/orm"
	"fmt"
	"cloud_worm/util/umodels/mysqldb"
)

type MachineInfo struct {
	Id          int64
	Ip          string          // 机器ip
	Name        string          // 项目名字和weapon name对应
	Nickname    string          // 项目展示名
	Count       int
	Detail      string

	Updated     int64
	Created     int64
	Status      int
}

func (w *MachineInfo) TableName() string {
	return reflect.TypeOf(*w).Name()
}

func (w *MachineInfo) Read(fields ...string) error {
	err := orm.NewOrm().Read(w, fields...)
	if err != nil {
		return fmt.Errorf("%s, %s", w.TableName(), err.Error())
	}
	return nil
}

func (w *MachineInfo) Update(fields ...string) error {
	_, err := orm.NewOrm().Update(w, fields...)
	if err != nil {
		return fmt.Errorf("%s, %s", w.TableName(), err.Error())
	}
	return nil
}

func (w *MachineInfo) Insert() error {
	_, err := orm.NewOrm().Insert(w)
	if err != nil {
		return fmt.Errorf("%s, %s", w.TableName(), err.Error())
	}
	return nil
}

func init()  {
	orm.RegisterModel(new(MachineInfo))
}

func (w *MachineInfo) Select(offset int, limit int, status int, ip string) ([]*MachineInfo, error) {

	var mInfos []*MachineInfo

	qs := orm.NewOrm().QueryTable(w).Offset(offset).Limit(limit).OrderBy("name")
	if ip != mysqldb.SelectStrAll {
		qs = qs.Filter("ip", ip)
	}
	if status != mysqldb.StatusAll {
		qs = qs.Filter("status", status)
	}

	_, err := qs.All(&mInfos)
	if err != nil {
		return nil, err
	}

	return mInfos, nil
}

func (w *MachineInfo) SelectCount(status int, ip string) (int64, error) {
	qs := orm.NewOrm().QueryTable(w)
	if ip != mysqldb.SelectStrAll {
		qs = qs.Filter("ip", ip)
	}
	if status != mysqldb.StatusAll {
		qs = qs.Filter("status", status)
	}

	count, err := qs.Count()
	if err != nil {
		return 0, err
	}

	return count, nil
}

func (w *MachineInfo) SelectByName(id int64, limit int, status int, name string) ([]*MachineInfo, error) {

	var mInfos []*MachineInfo

	qs := orm.NewOrm().QueryTable(w)
	if name != mysqldb.SelectStrAll {
		qs = qs.Filter("name", name)
	}
	if id != mysqldb.SelectIntAll {
		qs = qs.Filter("id__gt", id)
	}

	if status != mysqldb.StatusAll {
		qs = qs.Filter("status", status)
	}

	qs = qs.OrderBy("id").Limit(limit)

	_, err := qs.All(&mInfos)
	if err != nil {
		return nil, err
	}

	return mInfos, nil
}

func (w *MachineInfo) CountByName(status int, name string) (int64, error) {
	qs := orm.NewOrm().QueryTable(w)

	if name != mysqldb.SelectStrAll {
		qs = qs.Filter("name", name)
	}
	if status != mysqldb.StatusAll {
		qs = qs.Filter("status", status)
	}

	count, err := qs.Count()
	if err != nil {
		return 0, err
	}

	return count, nil
}

func (w *MachineInfo) NameDetailsForIPs(name string, ips []string) ([]*MachineInfo, error) {
	qs := orm.NewOrm().QueryTable(w)

	// 基于name选取
	if name != mysqldb.SelectStrAll {
		qs = qs.Filter("name", name)
	}
	//
	if len(ips) > 0 {
		qs = qs.Filter("ip__in", ips)
	}

	var mInfos []*MachineInfo
	_, err := qs.All(&mInfos)
	if err != nil {
		return nil, err
	}

	return mInfos, nil
}